﻿/// <reference path="Repeater.js" />
/// <reference path="EntryArgumentEventArgs.js" />
/// <reference path="../General/Json.js" />

var ____a;

var $RepeaterCommandEventArgs = TA.Turbo.Controls.RepeaterCommandEventArgs = function RepeaterCommandEventArgs(command, argument)
{
	this._command = command;
	this._argument = argument;
}
$RepeaterCommandEventArgs.Inherit(BaseJsonSerializable);

if (____a)
{
	$RepeaterCommandEventArgs.prototype = new BaseJsonSerializable();
	$RepeaterCommandEventArgs.prototype.BaseJsonSerializable = new BaseJsonSerializable();
}

$RepeaterCommandEventArgs.prototype._command = null;
$RepeaterCommandEventArgs.prototype._argument = null;

$RepeaterCommandEventArgs.prototype.get_Command = function get_Command()
{
	return this._command;
}
$RepeaterCommandEventArgs.prototype.get_Argument = function get_Argument()
{
	return this._argument;
}
$RepeaterCommandEventArgs.prototype.SerializeContents = function SerializeContents(jw)
{
	if (____a)
	{
		jw = new JsonWriter();
	}

	jw.Property("C", this.get_Command());
	jw.Property("A", this.get_Argument());
}


$Repeater.prototype.FireCommand = function FireCommand(command, arg)
{

	switch (command)
	{
		case "New":
			this.FireCommand_New();
			break;
		case "Edit":
			this.FireCommand_Edit(arg);
			break;
		case "Delete":
			this.FireCommand_Delete(arg);
			break;
		case "Save":
			this.FireCommand_Save(arg);
			break;
		case "SaveAll":
			for (var i = 0; i < this._items.length; ++i)
			{
				var item = this._items[i];
				if (item.get_Type() != $RepeaterItemType.Edit)
					continue;

				this.FireCommand_Save(item);
			}
			break;
		case "SaveCloseAll":
			for (var i = 0; i < this._items.length; ++i)
			{
				var item = this._items[i];
				if (item.get_Type() != $RepeaterItemType.Edit)
					continue;

				this.FireCommand_Save(item);
				this.FireCommand_Close(item);
			}
			break;
		case "Close":
			this.FireCommand_Close(arg);
			break;
		case "SaveClose":
			this.FireCommand_Save(arg);
			this.FireCommand_Close(arg);
			break;
		case "Sort":
			this.FireCommand_Sort(arg);
			break;
		default:
			if(arg instanceof $RepeaterItem)
			{
				arg = this.get_Entries()[arg.get_ValueIndex()];
			}
			
			var entryArgumentEventArgs = new $EntryArgumentEventArgs(arg);
			this.RaiseEvent("GetArgumentByEntry", entryArgumentEventArgs);
						
			
			this.RaiseEvent("Command", new $RepeaterCommandEventArgs(command, entryArgumentEventArgs.Argument));
			break;
	}
}


$Repeater.prototype.FireCommand_New = function FireCommand_New()
{
	if (this.get_Pager().Size != 0)
	{
		this.set_PageIndex(this.get_NumPages() - 1);
		this.UpdateDisplay();
	}
	var editItem = this._LeaseItem($RepeaterItemType.Edit, this);

	this._SetItemValue(editItem, null);
	editItem.FocusFirst();
}
$Repeater.prototype.FireCommand_Edit = function FireCommand_Edit(item)
{
	var editItem = this._LeaseItem($RepeaterItemType.Edit, item);
	this._ReleaseItem(item);

	this._SetItemValue(editItem, item.get_ValueIndex());
	editItem.FocusFirst();
}
$Repeater.prototype.FireCommand_Delete = function FireCommand_Delete(item)
{
	var vi = item.get_ValueIndex();
	if (vi == null)
	{
		this._ReleaseItem(item);
	}
	else
	{
		var v = this.get_Entries()[vi];

		this.get_Entries().splice(vi, 1);
		for (var i = 0; i < this._items.length; ++i)
		{
			if (this._items[i].get_ValueIndex() >= vi)
				this._items[i].set_ValueIndex(this._items[i].get_ValueIndex() - 1);
		}

		// if only one page (or no paging)
		if (this.get_Pager().Size == 0 || this.get_Pager().Size > this.get_Entries().length)
		{
			this._ReleaseItem(item); // just hide the item
		}
		else
		{
			this.UpdateDisplay();
		}

		this.RaiseEvent("ItemDeleted", { Argument: v });
	}
}
$Repeater.prototype.FireCommand_Save = function FireCommand_Save(item)
{
	if (!item.get_IsValid()) return;
	var v = this._GetItemValue(item);

	var sb = new StringBuilder();
	var jw = new JsonWriter(sb);
	this.get_Value().SerializeOneEntry(jw, v);
	var vSerialized = sb.toString();

	if (item.get_ValueIndex() == null)
	{ // new item
		this.get_Entries().push(v);
		this.get_Value().NumEntries++;
		item._valueIndex = this.get_Entries().length - 1;

		this.RaiseServerEvent("ItemAdded", vSerialized, null);
	}
	else
	{
		this.get_Entries()[item.get_ValueIndex()] = v;

		this.RaiseServerEvent("ItemUpdated", vSerialized, null);
	}

	this._OnValueChanged();
}
$Repeater.prototype.FireCommand_Close = function FireCommand_Close(item)
{
	if (item.get_Type() != $RepeaterItemType.Edit) return;
	if (item.get_ValueIndex() != null)
	{
		var viewItem = this._LeaseItem($RepeaterItemType.View, item);

		this._SetItemValue(viewItem, item.get_ValueIndex());
	}
	this._ReleaseItem(item);
}
$Repeater.prototype.FireCommand_Sort = function FireCommand_Sort(sortExpression)
{
	if (this.get_SortExpression() == sortExpression)
		this.set_SortDirection(1 - this.get_SortDirection());
	else
	{
		this.set_SortDirection(this.get_DefaultSortDirection());
		this.set_SortExpression(sortExpression);
	}
}
